<?php

header('Expires: Thu, 01 Jan 1970 00:00:01 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header("Content-type: text/html; charset=utf-8");
$code = $_GET["code"];
$returnUrl = "http://" . $_SERVER['HTTP_HOST'];
if ($code == "") {
    echo "none";
    exit;
}

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9d6ce34d0ab6bd64&secret=a87cb43b41d3812ed197a920c1566e06&code=" . $code . "&grant_type=authorization_code";
$data = "";
$tmpInfo = vpost($url, $data);
$j_arr = json_decode($tmpInfo, true);
$openid = $j_arr["openid"];
$access_token = $j_arr["access_token"];
$refresh_token = $j_arr["refresh_token"];
$url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$tmpInfo2 = vpost($url2, $data);
$u2_arr = json_decode($tmpInfo2, true);
$nickname = $u2_arr["nickname"];
$openid = $u2_arr["openid"];
$headimgurl = str_replace("wx.qlogo.cn/mmopen/", "", str_replace("http://", "", $u2_arr["headimgurl"]));
//echo $headimgurl;
//exit;
header("Location:" . $returnUrl . "/hongbao/Mobile/GetInfo/getInfo?openid=" . $openid . "&nickname=" . $nickname . "&headimgurl=" . $headimgurl);

function vpost($url, $data)
{ // 模拟提交数据函数
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
    // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
        echo 'Errno' . curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}

?>